import { getRepository } from 'typeorm';
import { ResumeEntity } from '../entity/resume.entity';
import { simpleUUID } from '../utils/uuid';

const resumeRepository = getRepository(ResumeEntity);

/**
 * 添加简历
 * @param entity 简历信息
 * @returns
 */
export async function saveOnline(entity: ResumeEntity) {
  const resume = await resumeRepository.findOne({ name: entity.name, type: entity.type });
  if (resume) {
    const { affected } = await resumeRepository.update(resume.id, entity);
    return affected;
  }
  entity.id = simpleUUID();
  const { raw } = await resumeRepository.insert(entity);
  return raw.affectedRows;
}

export async function select(uid: string) {
  return await resumeRepository.find({ uid });
}
